package com.houdoudou.com.machine;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonFunction;

public class MachineDistributeManagement {

	
	public static JSONObject manage(String jsonData){
		JSONObject result = new JSONObject();
		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
		//判断请求的功能
		int FunctionCode=jsonfrom.getInt("FunctionCode");
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction. getConnection();
		   // 此值代表用户身份，不同的身份，对于同一个增删改查函数，有不同的处理，防止出错。
		   int Manager_Authority= jsonfrom.getInt("Manager_Authority");
		   switch(FunctionCode){
		   
		   // 删除 
			case 3:{	
				     // 只允许业主删
				if(Manager_Authority==4){
					
					 JSONArray Distributes= jsonfrom.getJSONArray("Distributes");
					 for(int i=0;i<Distributes.size();i++){
					 try{						
						 String Supplier_Telnum=Distributes.getJSONObject(i).getString("Supplier_Telnum");
						  int  Machine_ID=Distributes.getJSONObject(i).getInt("Machine_ID");
				//		 String Owner_Telnum=jsonfrom.getString("Owner_Telnum");
						 String Time_Flag=Distributes.getJSONObject(i).getString("Time_Flag");
					  
						 String	sql	=" delete from  Tb_Machine_Supplier where Machine_ID="+Machine_ID+" and Supplier_Telnum="+Supplier_Telnum+" and Time_Flag="+Time_Flag+"";									   
						             stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);  							           
						            		 stm.executeUpdate(sql);  	
						     				
												result.put("ActionStatus", true);
												result.put("ErrorInfo", "删除成功");
									} catch (SQLException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
										result.put("ActionStatus", false);
										result.put("ErrorInfo", "数据库删除操作失败");
									    break;
									} 							
							
					 }// end of for	
						
			}else{
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "你没有权限");}
			
					   break;     } //case 3
		   
		   
		   // 新增
			case 4:{
				 if(Manager_Authority==4){
						// step2 检查是否已经存在
					 JSONArray Distributes= jsonfrom.getJSONArray("Distributes");
					 for(int i=0;i<Distributes.size();i++){
					 try{						
						 String Supplier_Telnum=Distributes.getJSONObject(i).getString("Supplier_Telnum");
						  int  Machine_ID=Distributes.getJSONObject(i).getInt("Machine_ID");
						 String Owner_Telnum=jsonfrom.getString("Owner_Telnum");
						 String Time_Flag=Distributes.getJSONObject(i).getString("Time_Flag");
						 
			             String sql0="if not exists(select 1 from  Tb_Machine_Supplier where Machine_ID="+Machine_ID+" and Supplier_Telnum="+Supplier_Telnum+" and Time_Flag="+Time_Flag+")";					
						 String sql1 = "insert into Tb_Machine_Supplier (Machine_ID,Supplier_Telnum,Time_Flag,Owner_Telnum) values ("+Machine_ID+","+Supplier_Telnum+","+Time_Flag+","+Owner_Telnum+")";
						 String sql=sql0+sql1;
			            System.out.println("add distribute,sql="+sql);
						 stm=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			              stm.execute(sql);
			              // 多次赋值，同名覆盖
			              result.put("ActionStatus", true);
						  result.put("ErrorInfo", "增加成功");	  		
						
					 } catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "数据库异常");
							break;
						}
					 }// end of for
					   	
						 }// end of if manager_authority==4
						 else{
							 result.put("ActionStatus", false);
								result.put("ErrorInfo", "你没有权限操作");}			
			   break;}// case 4
		   
		   
		 //查询所有
			 case 5:{
				  try {
						JSONArray Distributes= new JSONArray();	 
						int Query_Way=jsonfrom.getInt("Query_Way");
						switch(Query_Way){
						//方式1 查询所有属于供应商的机器分配情况。
						case 1:{
					   String sql = "select * from  Tb_Machine_Supplier";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
					
						while(rs.next()){
							JSONObject distribute= new JSONObject();
							boolean isadd=false;
						switch(Manager_Authority){				
						
						case 4:{
							if(rs.getString("Owner_Telnum").equals(jsonfrom.getString("Owner_Telnum"))) 
								isadd=true;
							        break;}	
			              }
						if(isadd){
							distribute.put("Supplier_Telnum", rs.getString("Supplier_Telnum"));
							distribute.put("Machine_ID", rs.getInt("Machine_ID"));
							distribute.put("Time_Flag", rs.getString("Time_Flag"));
	                        Distributes.add(distribute);               
						                         }
						
						}
						result.put("ActionStatus", true);
						result.put("ErrorInfo", "成功");
						result.put("Distributes",Distributes);
						 break;}// end of case 1 
						
						
						//方式2 按。Machine_ID查询所有的供应商
						case 2:{
							int Machine_ID=jsonfrom.getInt("Machine_ID");
					   String sql = "select * from  Tb_Machine_Supplier where Machine_ID="+Machine_ID+"";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
					
						while(rs.next()){
							JSONObject distribute= new JSONObject();
							boolean isadd=false;
						switch(Manager_Authority){
						
						
						case 4:{
							if(rs.getString("Owner_Telnum").equals(jsonfrom.getString("Owner_Telnum"))) 
								isadd=true;
							        break;}				          					
			              }
						if(isadd){
							distribute.put("Supplier_Telnum", rs.getString("Supplier_Telnum"));
							distribute.put("Machine_ID", rs.getInt("Machine_ID"));
							distribute.put("Time_Flag", rs.getString("Time_Flag"));
	                        Distributes.add(distribute);               
						                         }
						
						}
						result.put("ActionStatus", true);
						result.put("ErrorInfo", "成功");
						result.put("Distributes",Distributes);
						 break;}// end of case 2 
						
						//方式3 按。Supplier_Telnum 查询所有机器
						case 3:{
							String  Supplier_Telnum=jsonfrom.getString("Supplier_Telnum");
					   String sql = "select * from  Tb_Machine_Supplier where Supplier_Telnum="+Supplier_Telnum+"";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
					
						while(rs.next()){
							JSONObject distribute= new JSONObject();
							boolean isadd=false;
						switch(Manager_Authority){
						
						
						case 4:{
							if(rs.getString("Owner_Telnum").equals(jsonfrom.getString("Owner_Telnum"))) 
								isadd=true;
							        break;}	
			              }
						if(isadd){
							distribute.put("Supplier_Telnum", rs.getString("Supplier_Telnum"));
							distribute.put("Machine_ID", rs.getInt("Machine_ID"));
							distribute.put("Time_Flag", rs.getString("Time_Flag"));
	                        Distributes.add(distribute);               
						                         }
						
						}
						result.put("ActionStatus", true);
						result.put("ErrorInfo", "成功");
						result.put("Distributes",Distributes);
						 break;}// end of case 3 
					
						//方式4按。Supplier_Telnum Machine_ID 查单个机器
						case 4:{
							String  Supplier_Telnum=jsonfrom.getString("Supplier_Telnum");
							int Machine_ID=jsonfrom.getInt("Machine_ID");
					   String sql = "select * from  Tb_Machine_Supplier where Supplier_Telnum="+Supplier_Telnum+" and Machine_ID="+Machine_ID+"";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
					
						while(rs.next()){
							JSONObject distribute= new JSONObject();
							boolean isadd=false;
						switch(Manager_Authority){
						
						
						case 4:{
							if(rs.getString("Owner_Telnum").equals(jsonfrom.getString("Owner_Telnum"))) 
								isadd=true;
							        break;}	
			              }
						if(isadd){
							distribute.put("Supplier_Telnum", rs.getString("Supplier_Telnum"));
							distribute.put("Machine_ID", rs.getInt("Machine_ID"));
							distribute.put("Time_Flag", rs.getString("Time_Flag"));
	                        Distributes.add(distribute);               
						                         }
						
						}
						result.put("ActionStatus", true);
						result.put("ErrorInfo", "成功");
						result.put("Distributes",Distributes);
						 break;}// end of case 4
						
						//方式5 按。Machine_ID和 Time_Flag 查询所有的供应商
						case 5:{
							String Time_Flag=jsonfrom.getString("Time_Flag");
							int Machine_ID=jsonfrom.getInt("Machine_ID");
					   String sql = "select * from  Tb_Machine_Supplier where Machine_ID="+Machine_ID+"and Time_Flag="+Time_Flag+"";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
					
						while(rs.next()){
							JSONObject distribute= new JSONObject();
							boolean isadd=false;
						switch(Manager_Authority){						
						case 6:{
								isadd=true;
							        break;}				          					
			                  }
						if(isadd){
							distribute.put("Supplier_Telnum", rs.getString("Supplier_Telnum"));
							distribute.put("Machine_ID", rs.getInt("Machine_ID"));
							distribute.put("Time_Flag", rs.getString("Time_Flag"));
	                        Distributes.add(distribute);               
						                         }
						
						}
						result.put("ActionStatus", true);
						result.put("ErrorInfo", "成功");
						result.put("Distributes",Distributes);
						 break;}// end of case 2 
						
						
						}// switch(Query_Way)
				   
				  }catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "查询失败");
					} 
						
						
					 break;  } //case5
		   
		   }// end switch(functionCode)
            CommonFunction.closeConn(conn, pstm, stm, rs);
		   
		   return result;
		   
	}
}
